<template>
  <div class="container">
    <el-page-header @back="goBack" content="混凝土施工台账（标养）">
    </el-page-header>

    <div class="search">
      <span style="line-height: 100%">工程名称：</span>
      <el-input v-model="projectName" placeholder="请输入内容" style="width: 300px"/>
      <span style="line-height: 100%; margin-left: 10px">单位工程：</span>
      <el-input v-model="subprojectName" placeholder="请输入内容" style="width: 300px"/>
      <el-button type="primary" style="margin-left: 10px;margin-right: 10px" @click="search">搜索</el-button>
      <el-button type="warning" style="margin-left: 10px;margin-right: 10px;" @click="batchExport">批量导出</el-button>
    </div>

    <el-table :data="currentPageData" stripe style="width: 100%; margin-top: 15px"
              @selection-change="handleSelectionChange" row-key="taskId" ref="tableRef"
    >
      <!-- 第一列：多选框 -->
      <el-table-column type="selection" width="55" :reserve-selection="true"/>
      <!-- 第二列：编号 -->
      <el-table-column prop="projectId" label="工程序号" width="250" align="center"/>
      <el-table-column prop="projectName" label="工程名称" width="400" align="center"/>
      <el-table-column prop="subprojectId" label="子工程序号" width="400" align="center"/>
      <el-table-column prop="subprojectName" label="子工程名称" width="400" align="center"/>


      <!-- 最后一列：操作按钮 -->
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="handlePreview(scope.row)">
            预览
          </el-button>
          <el-button type="warning" @click="handleExport(scope.row)">
            导出
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 这是分页条  -->
    <el-pagination style="margin-top: 20px; text-align: center" :current-page="currentPage"
                   :page-sizes="[1, 10, 15, 20]" :page-size="pageSize" layout="prev, pager, next, jumper,->,sizes,total"
                   :total="totalData" @size-change="handleSizeChange" @current-change="handleCurrentChange"
    />

  </div>
</template>

<script>
import { searchLedgerTable, exportConcreteLedgerByTemplate } from '@/api/statementManage/hunNingTu/hunNingTu'
import { downloadFile } from '@/utils/fileDownload'

export default {
  data() {
    return {
      projectName: '',
      subprojectName: '', //这三个用来搜素表格项
      currentPageData: [],  //当前页面数据
      currentPage: 1,
      pageSize: 10,
      totalData: 0,   //这三个是分页条数据
      selectedRows: []  //选中行数组
    }
  },
  methods: {
    goBack() {
      this.$router.go(-1)
    },
    handleSizeChange(val) {
      this.pageSize = val
      console.log('表格容量更新为：' + val)
      this.search()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      console.log('表格页数更新为：' + val)
      this.search()
    },
    handlePreview(row) {
      console.log('预览表格行数据为：' + JSON.stringify(row))

      const keyData = {
        projectId: row.projectId,
        subprojectId: row.subprojectId,
        projectName: row.projectName,
        subprojectName: row.subprojectName
      }

      console.log(JSON.stringify(keyData))

      sessionStorage.setItem('tempObj1', JSON.stringify(keyData))

      this.$router.push({ path: '/report-viewer/concreteBiaoYang' })

    },
    async handleExport(row) {
      try {
        if (!row.subprojectId) {
          this.$message.warning('缺少必要的导出参数')
          return
        }
        const file = await exportConcreteLedgerByTemplate(row.subprojectId, '标准养护')
        await downloadFile(file, `混凝土台账_${row.subprojectId}_标准养护.xlsx`)
        this.$message.success('导出成功')
      } catch (e) {
        console.error('导出失败:', e)
        this.$message.error('导出失败：' + (e.message || '该报表不可导出'))
      }
    },
    handleSelectionChange(val) {
      this.selectedRows = val
      console.log('当前选中行为：' + JSON.stringify(val))
    },
    async search() {
      const { result } = await searchLedgerTable(this.currentPage, this.pageSize, this.projectName, this.subprojectName, '标准养护')
      console.log(JSON.stringify(result))
      this.currentPageData = result.list
      this.totalData = Number(result.total)
    },
    batchExport() {
      console.log('进行了批量导出')
    }
  },
  mounted() {
    this.search()
  }
}
</script>

<style scoped>
.container {
  margin-top: 10px;
  margin-left: 10px;
}

.search {
  text-align: right;
  margin-top: 20px;
}
</style>
